package com.meichis.yndms.ui.activity;

import android.graphics.Color;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.google.gson.Gson;
import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.yndms.R;
import com.meichis.yndms.databinding.ActivityPartnerProductMarketRateBinding;
import com.meichis.yndms.model.ProductMarketRate;
import com.meichis.yndms.model.ProductMarketRate_Echarts;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.PartnerProductMarketRateVM;

import java.util.ArrayList;
import java.util.List;

public class PartnerProductMarketRateActivity extends BaseActivity {

    private ActivityPartnerProductMarketRateBinding binding;
    private PartnerProductMarketRateVM vm;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_partner_product_market_rate);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(PartnerProductMarketRateVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("铺市率");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        vm.GetPartnerProductMarketRate();

        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getProductMaarketRateList().observe(this, productMarketRates -> {

            List<ProductMarketRate_Echarts> list = new ArrayList<>();
            for (ProductMarketRate rate : productMarketRates) {
                ProductMarketRate_Echarts p = new ProductMarketRate_Echarts();
                p.setBrand(rate.getBrand());
                p.setMarketCount(rate.getCurrentMonthMarketCount());
                p.setRate(rate.getCurrentMonthMarketRate());
                p.setSumCount(rate.getCurrentMonthCount());
                list.add(p);
            }

            binding.web.setBackgroundColor(Color.parseColor("#ffffff"));
            //加载本地资源文件
            binding.web.loadUrl("file:///android_asset/PartnerProductMarketRate.html");
            binding.web.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    binding.web.sendResult("", "JavaCallJs", new Gson().toJson(list));
                }
            });
        });
    }


    public class ClickEvent {

        public void onSelectDateType() {
            new DropDownDialog<>(PartnerProductMarketRateActivity.this, R.color.theme, "时间维度", "Name", vm.getDataTableList(), (o, position) -> {
                vm.getDataTable().setValue(vm.getDataTableList().get(position));
                List<ProductMarketRate_Echarts> list = new ArrayList<>();

                for (ProductMarketRate rate : vm.getProductMaarketRateList().getValue()) {
                    ProductMarketRate_Echarts p = new ProductMarketRate_Echarts();
                    p.setBrand(rate.getBrand());

                    switch (vm.getDataTableList().get(position).getValue()) {
                        case 1:
                            p.setSumCount(rate.getCurrentMonthCount());
                            p.setMarketCount(rate.getCurrentMonthMarketCount());
                            p.setRate(rate.getCurrentMonthMarketRate());
                            break;
                        case 3:
                            p.setSumCount(rate.getMonthlyCount());
                            p.setMarketCount(rate.getMonthlyMarketCount());
                            p.setRate(rate.getMonthlyMarketRate());
                            break;
                        case 5:
                            p.setSumCount(rate.getQuarterCount());
                            p.setMarketCount(rate.getQuarterMarketCount());
                            p.setRate(rate.getQuarterMarketRate());
                            break;
                    }
                    list.add(p);
                }
                binding.web.sendResult("", "JavaCallJs", new Gson().toJson(list));
            }).show();
        }
    }
}
